#include "camera.h"

	   int 		camera_photo_flag			;	//摄像头拍照标志位(六)
	   struct 	jpg_data video_buf			;	//定义结构体变量，摄像头数据	
	   char     photo_jpg_buf[128]  =  {0}  ;
	   int      photo_jpg_num		=	0	;

//1、打开摄像头
void Camera_Open(char *pathname_camera)
{
	//a、打开开发板屏幕、申请开发板虚拟内存
	Jpeg_Lcd_Open();
	//b、初始化摄像头
	linux_v4l2_yuyv_init(pathname_camera);
	//c、开启摄像头捕捉
	linux_v4l2_start_yuyv_capturing();
	return ;
}

//2、运行摄像头(600*480)
void Camera_Show(int lcd_x, int lcd_y)
{
	while(1)
	{
		if (page==page_main)
		{
			//a、获取摄像头捕捉的画面数据(一直读)
			linux_v4l2_get_yuyv_data(&video_buf);

			//b、显示摄像头捕捉的画面(一直写)
			Jpeg_Lcd_Show_Camera(lcd_x, lcd_y, video_buf.jpg_data , video_buf.jpg_size);

			if (camera_photo_flag == 1)
			{

				//1、打开jpg图片文件(以只写、创建的方式打开)
				bzero(photo_jpg_buf, sizeof(photo_jpg_buf));
				sprintf(photo_jpg_buf, "./photo/%d.jpg", photo_jpg_num);
				int jpg_fd = open(photo_jpg_buf, O_WRONLY|O_CREAT|O_TRUNC);
				//2、将摄像头数据写到jpg图片上
				write(jpg_fd, video_buf.jpg_data, video_buf.jpg_size);
				Bmp_Show(240, 120, 400, 240, "./white.bmp");
				sleep(0.5);
				//3、关闭jpg图片文件
				close(jpg_fd);

				photo_jpg_num++;
				camera_photo_flag = 0;
			}
		}
	}
}

//3、运行摄像头线程函数
void *Camera_Show_Pt(void *arg)
{

	Camera_Show(240, 120);
	pthread_exit(NULL);
	return (void *)0;
}



//4、关闭摄像头
void Camera_Close(void)
{
	//a、摄像头解除初始化
	linux_v4l2_yuyv_quit();
	
	//b、关闭摄像头
	Jpeg_Lcd_Close();
	return ;
}



